﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AssettDatabase
{
    public partial class Form_Modify_Supplier : Form
    {
        public Form_Modify_Supplier()
        {
            InitializeComponent();
        }

        private void Btn_Return_Main_Click(object sender, EventArgs e)
        {
            this.Dispose();
            FormProvider.MainMenu.Show();
        }

        private void Form_Modify_Supplier_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Dispose();
            FormProvider.MainMenu.Show();
        }

        void AutoCompleteText()
        {
            try
            {
                BLL.Supplier bllSupplier = new BLL.Supplier();
                AutoCompleteStringCollection strCollection = new AutoCompleteStringCollection();
                TB_Supplier_Name.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                TB_Supplier_Name.AutoCompleteSource = AutoCompleteSource.CustomSource;
                TB_Supplier_Name.AutoCompleteCustomSource = strCollection;

                List<VO.Supplier> voSupplierList = bllSupplier.GetSupplier();

                foreach (VO.Supplier supplier in voSupplierList)
                {
                    strCollection.Add(supplier.Supplier_Name);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Form_Modify_Supplier_Load(object sender, EventArgs e)
        {
            AutoCompleteText();
        }

        private void Supplier_Search(string Supplier_ID)
        {
            try
            {
                BLL.Supplier bllSupplier = new BLL.Supplier();
                List<VO.Supplier> voSupplierList;
                if (Supplier_ID != null)
                    voSupplierList = bllSupplier.GetSupplierById(Convert.ToInt32(Supplier_ID));
                else
                    voSupplierList = bllSupplier.GetSupplierByName(TB_Supplier_Name.Text);

                foreach (VO.Supplier supplier in voSupplierList)
                {
                    MS_TB_CustomerCode.Text = supplier.Supplier_Customer_Code;
                    MS_TB_Primary_Contact_Email.Text = supplier.Sup_Primary_Contact_EML;
                    MS_TB_Primary_Contact_FN.Text = supplier.Sup_Primary_Contact_FN;
                    MS_TB_Primary_Contact_LN.Text = supplier.Sup_Primary_Contact_LN;
                    MS_TB_Supplier_Phone.Text = supplier.Supplier_Phone;
                    MS_TB_SupplierComments.Text = supplier.Sup_Comments;
                    MS_TB_Web_Password.Text = supplier.Sup_Web_Password;
                    MS_TB_Web_UserName.Text = supplier.Sup_Web_Username;
                    MS_TB_Webstie.Text = supplier.Supplier_Website;
                    TB_Supplier_Name.Text = supplier.Supplier_Name;
                    TBRT_Customer_ID.Text = supplier.Supplier_ID.ToString();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void BT_Search_Click(object sender, EventArgs e)
        {
            using (var form = new Form_Sel_Supplier())
            {
                var result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    Supplier_Search(form.retSupplier_ID);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                BLL.Supplier supplier = new BLL.Supplier();
                if (!supplier.Update(TB_Supplier_Name.Text, MS_TB_Supplier_Phone.Text, MS_TB_Primary_Contact_FN.Text, MS_TB_Primary_Contact_LN.Text, MS_TB_Primary_Contact_Email.Text,
                                    MS_TB_Webstie.Text, MS_TB_Web_UserName.Text, MS_TB_Web_Password.Text, MS_TB_CustomerCode.Text, MS_TB_SupplierComments.Text, Convert.ToInt32(TBRT_Customer_ID.Text)))
                    MessageBox.Show("An error occurred during customer update");
                else
                    MessageBox.Show(TB_Supplier_Name.Text + " has been updated");
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void TB_Supplier_Name_KeyDown(object sender, KeyEventArgs e)
        {
            if (TB_Supplier_Name.Text.Length != 0)
                if (e.KeyCode == Keys.Enter)
                {
                    Supplier_Search(null);
                }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dialog = MessageBox.Show("Do you really want to Delete the Supplier " + this.TB_Supplier_Name.Text + "?", "Delete Supplier", MessageBoxButtons.YesNo);
                if (dialog == DialogResult.Yes)
                {
                    BLL.Supplier supplier = new BLL.Supplier();
                    if (!supplier.Del(Convert.ToInt32(TBRT_Customer_ID.Text)))
                        MessageBox.Show("An error occurred during supplier delete");
                    else
                        MessageBox.Show(TB_Supplier_Name.Text + " has been deleted");
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
